package com.mod.enemies 
{
	import com.calefk.core.EntitySimple;
	import com.calefk.core.List;
	import com.calefk.core.Project;
	import com.calefk.core.Task;
	import com.calefk.modules.path.Node;
	import com.calefk.modules.path.PathFinding;
	import com.calefk.util.helper;
	import com.calefk.util.Iterator;
	import com.mod.Global;
	import flash.display.MovieClip;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class EnemyChase extends Enemy {
		
		public static const ST_APPEAR:int = 0;
		public static const ST_FOLLOW:int = 1;
		public static const ST_DELAY :int = 2;
		public static const ST_GOSTRAIGTH:int = 3;
		
		private var mTasks:List;
		// ruta para el enemigo
		private var mPathFollow:Array;
		private var mIterator:Iterator;
		private var mMovement:Movement;
		
		private var mIsCollUp:Boolean;
		private var mIsCollDown:Boolean;
		private var mIsCollRight:Boolean;
		private var mIsCollLeft :Boolean;
		private var mWidth :Number = 60;
		private var mHeight:Number = 60;
		
		private var rect_a:Rectangle = new Rectangle();
		private var rect_b:Rectangle = new Rectangle();
		
		private var newX:Number;
		private var newY:Number;
		
		public function EnemyChase(canvas:MovieClip, nameClip:String = null,
				px:Number=0, py:Number=0, project:Project=null) {
			super(canvas, nameClip, px, py, project);
			
			mTasks = new List();
			
			onAppear();
			newX = x;
			newY = y;
			
		}
		
		private function onAppear():void {
			mState = ST_GOSTRAIGTH;
			//mTasks.add(new Task(endAppear , 200));
		}
		
		override public function update(dt:int):void {
			if (mTasks) {
				mTasks.update(dt);	
			}
			/* */
			switch (mState) {
				case ST_APPEAR:
					break;
				case ST_FOLLOW:
					
					if (mMovement) {
						mMovement.update(dt);
					}
					
					break;
				case ST_GOSTRAIGTH:
					{
						//trace("followplayer");
						followPlayer();
					}
					break;
				case ST_DELAY:
					break;
			}
		}
		
		private function followPlayer():void 
		{
			mIsCollUp = false;
			mIsCollDown = false;
			mIsCollRight = false;
			mIsCollLeft = false;
			
			var dx:Number = Global.engine.player.x - x;
			var dy:Number = Global.engine.player.y - y;
			
			var vx:Number = dx * 0.1;
			var vy:Number = dy * 0.1;
			
			var dst:Number = Math.sqrt(vx * vx + vy * vy);
			
			if (dst > mMaxSpeed) {
				vx = vx / dst * mMaxSpeed;
				vy = vy / dst * mMaxSpeed;
			}
			
			newX += vx;
			newY += vy;
			
		
			rect_a.x = newX - (mWidth  * 0.5);
			rect_a.y = newY - (mHeight * 0.5);
			rect_a.width  = mWidth;
			rect_a.height = mHeight;
			
			var mc_hearth:MovieClip = Global.engine.world.hearth;
			rect_b.x = mc_hearth.x - (mc_hearth.width  * 0.5);
			rect_b.y = mc_hearth.y - (mc_hearth.height * 0.5);
			rect_b.width  = mc_hearth.width;
			rect_b.height = mc_hearth.height;
			
			var rect_coll:Rectangle = rect_a.intersection(rect_b);
			
			if (rect_coll.width > 0 || rect_coll.height > 0) {
				var oldBottomY:Number =  y + (mHeight * 0.5);
				var oldTopY   :Number =  y - (mHeight * 0.5);
				
				var oldRightX:Number   =  x + (mHeight * 0.5);
				var oldleftX   :Number =  x - (mHeight * 0.5);
				
				if (oldTopY >= rect_b.bottom) {
					newY = rect_b.bottom + (mHeight * 0.5) + 1;
					mIsCollUp = true;
				}
				
				else if (oldBottomY <= rect_b.top) {
					newY = rect_b.top - ((mHeight * 0.5)  + 1);
					mIsCollDown = true;
				}
				
				if (oldleftX >= rect_b.right) {
					newX = rect_b.right + ((mWidth * 0.5) + 1);
					mIsCollLeft = true;
				}
				
				else if (oldRightX <= rect_b.left) {
					newX = rect_b.left - ((mWidth * 0.5) + 1);
					mIsCollRight = true;
				}
			}
			if (mIsCollDown || mIsCollRight || mIsCollLeft || mIsCollUp)
			{
				mState = ST_FOLLOW;
				onFollow();
			}else
			{
				y = newY;
				x = newX;
			}
		}
		
		private function endAppear():void {
			onFollow();
			/*followPlayer();
			mState = ST_GOSTRAIGTH;*/
		}
		
		private function onFollow():void {
			mState = ST_FOLLOW;
			/* Obtener Ruta Pathfinding */
			var column:int = mClip.x / Global.TILE_WIDTH;
			var row   :int = mClip.y / Global.TILE_HEIGHT;
			
			var player_column:int = Global.engine.player.x / Global.TILE_WIDTH;
			var player_row   :int = Global.engine.player.y / Global.TILE_HEIGHT;
			
			var pathfinding:PathFinding = new PathFinding();
			mPathFollow = PathFinding.getPath(Global.engine.enemyManager.mapbits , row , column , player_row , player_column , 0);
			
			if (mPathFollow != null && mPathFollow.length > 2) {
				mPathFollow.splice(0 , 1);
				mPathFollow.splice(mPathFollow.length , 1);
				mIterator = new Iterator(mPathFollow);
				
				if (mIterator) {
					if (mIterator.hasNext()) {
						var node:Node = mIterator.next();
						var endX:Number = (node.j * Global.TILE_WIDTH) + (Global.TILE_WIDTH * 0.5);
						var endY:Number = (node.i * Global.TILE_HEIGHT) + (Global.TILE_HEIGHT * 0.5);
						//if (!helper.LineIntersectsRect(new Point(this.x, this.y), new Point(endX, endY), rect_b))
						//{
						//	mState=ST_GOSTRAIGTH
						//}else{
							mMovement = new Movement(this , mClip.x , mClip.y , endX , endY , 0.2);
							mMovement.onEndMovement = onEndMovement;
						//}
					}
				}	
			} else {
				onDelay();
			
			}
		}
		
		private function onEndMovement():void {
			if (mIterator) {
				if (mIterator.hasNext()) {
					var node:Node = mIterator.next();
					var endX:Number = (node.j * Global.TILE_WIDTH) + (Global.TILE_WIDTH * 0.5);
					var endY:Number = (node.i * Global.TILE_HEIGHT) + (Global.TILE_HEIGHT * 0.5)
					//if (!helper.LineIntersectsRect(new Point(this.x, this.y), new Point(endX, endY), rect_b))
					//{
					//	mState=ST_GOSTRAIGTH
					//}else{
						mMovement = new Movement(this , mClip.x , mClip.y , endX, endY, 0.2);
						mMovement.onEndMovement = onEndMovement;
					//}
				} else {
					onDelay();
				}
			}			
		}
		
		private function onDelay():void {
			trace( "ST_DELAY : " + ST_DELAY );
			mState = ST_DELAY;
			mTasks.add(new Task(endDelay , 100));			
		}
		
		private function endDelay():void {
			if (!helper.LineIntersectsRect(new Point(this.x, this.y), new Point(mMovement.mEndX, mMovement.mEndY), rect_b))
			{
				mState = ST_GOSTRAIGTH;
				newX = x;
				newY = y;
			}else
				onFollow();
		}
		
		override public function clean():void {
			mIterator = null;
			mMovement = null;
			super.clean();
		}
		
	}

}